$(function() {
	//返回上一页
	var $back = $('.back');
	$back.on('click', function() {
		var hislen = window.history.length;
		hislen > 1 ? (window.history.back()) : window.location.href = 'index.html';
	})

	//首页底部菜单active处理
	var $tabbar = $(".tabbar-item");
	$tabbar.each(function() {
		if($(this).hasClass('tabbar-active')) {
			//console.log($(this));
			$(this).find('span').eq(0).addClass("hide").siblings().removeClass('hide');
		}
	});

	var dialog = YDUI.dialog;
	//添加到购物车
	var lists = $(".recommend-item-list");
	lists.on('click', '.item-add', function(e) {
		e.preventDefault();
		dialog.toast('已加入购物车', 'success', 1000);
		console.log($(this));
	})

	//	//获取地址栏参数
	//	function GetUrlPara(name) {
	//		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	//		var r = window.location.search.substr(1).match(reg);
	//		if(r != null) {
	//			return unescape(r[2]);
	//		}
	//		return null;
	//	}

});

var bingUtil = {
	//获取地址栏参数
	GetUrlPara: function(name) {
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
		var r = window.location.search.substr(1).match(reg);
		if(r != null) {
			return unescape(r[2]);
		}
		return null;
	},
	//数据请求
	getData: function(opt) {
		console.log(opt);
		dialog.loading.open('加载中...');
		$.ajax({
			type: "post",
			url: url,
			data: opt.data,
			dataType: 'jsonp',
			success: function(res) {
				dialog.loading.close();
				console.log(res);
				opt.callBack && opt.callBack(res);
			},
			error: function() {
				dialog.toast('获取订单失败，请检查网络！', 'error');
			}
		});
	},
	//计算时间差 @params endtime --结束时间时间戳
	getRemainTime(endtime){
		var	curtime=Date.parse(new Date())/1000;
		var remaintime=endtime-curtime;

		var seconds=~~((remaintime)% 60),
            minutes=~~((remaintime/60)% 60),
            hours=~~(remaintime/60/60);
		return {
			"curtime":new Date(),
			"endtime":new Date(endtime*1000),
			"total":remaintime,
			"hour":('0'+hours).slice(-2),
			"minute":('0'+minutes).slice(-2),
			"second":('0'+seconds).slice(-2)
		}
	},
    getTimeClock(endtime,callback){
		var remainTime=bingUtil.getRemainTime(endtime);
		console.log(remainTime);
		var $hour=$(".timedown .hours");
		var $minute=$(".timedown .minutes");
		var $second=$(".timedown .seconds");

		$hour.html(remainTime.hour);
        $minute.html(remainTime.minute);
        $second.html(remainTime.second);

        var oTimer=setTimeout(function(){bingUtil.getTimeClock(endtime,callback)},1000);
        if(remainTime.total<=0){
        	clearTimeout(oTimer);
            /*$hour.html('00');
            $minute.html('00');
            $second.html('00');*/

            callback && callback();
		}
	}

}